package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Facility;

import BizLogic.BookingManager;
import BizLogic.FacilityManager;

/**
 * Servlet implementation class CheckAvailableFacilityServlet
 */
@WebServlet("/CheckAvailableFacilityServlet")
public class CheckAvailableFacilityServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CheckAvailableFacilityServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(response, request);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(response, request);
	}

	private void processRequest(HttpServletResponse response,
			HttpServletRequest request) throws ServletException, IOException {
		// TODO Auto-generated method stub
		BookingManager bm = new BookingManager();
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dt1=null;
		java.util.Date dt2=null;
		try {
			dt1=sdf.parse(request.getParameter("datepicker"));
			System.out.println(dt1);
			dt2=sdf.parse(request.getParameter("datepickers"));
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date FromDate=Date.valueOf(sdf1.format(dt1));
		Date ToDate=Date.valueOf(sdf1.format(dt2));
		String fn=request.getParameter("facility");
		if (bm.detectOverLap(FromDate, ToDate, fn)) {
			PrintWriter out=response.getWriter();
			out.println("<script>alert('Not Available for selected time slot')</script>");
			/*FacilityManager fm=new FacilityManager();
			ArrayList<String> facility=fm.getFacilityNames();
			request.setAttribute("result", facility);*/
			RequestDispatcher rd1 = request
					.getRequestDispatcher("LoadSearchFacilities.jsp");
			rd1.forward(request, response);

		} else {
			FacilityManager fm = new FacilityManager();
			Facility facility1=fm.findbyName(fn);
			System.out.println(facility1.getFacType().getFacilityType());
			request.setAttribute("facility", facility1);
			RequestDispatcher rd1 = request
					.getRequestDispatcher("SearchFacResult.jsp");
			rd1.forward(request, response);
		}
	}

}
